<script lang="ts" setup name="DateShortcut">
import { formatDate } from "@/utils/date-formatter";
import {
  getDaysInCurrentMonth,
  weekDay,
  getDaysSinceSeasonStart,
  getDaysInLastMonth,
  getDaysSinceStartOfYear,
  getLastQuarterDays,
  getDaysInLastYear
} from "@/utils/date";
import { commandList } from "@/utils/serviceDict";
interface Emits {
  (name: "changeDate", value: string[]): void;
}
const emits = defineEmits<Emits>();
// 修改列表
const changeCommand = (e: number) => {
  let time = ["2024-11-05 00:00", "2024-12-10 00:00"];
  let start = 0; // 可变日期 （前面）
  let end = 0; // 当天日期（后面）

  switch (e) {
    case 1:
      // 今天
      start = 0;
      break;
    case 2:
      // 本周
      start = -weekDay() + 1;
      break;
    case 3:
      // 本月
      start = -getDaysInCurrentMonth() + 1;
      break;
    case 4:
      // 本季度
      start = -getDaysSinceSeasonStart() + 1;
      break;
    case 5:
      // 本年
      start = -getDaysSinceStartOfYear();
      console.log(getDaysSinceStartOfYear());
      break;
    case 6:
      // 昨天
      start = -1;
      break;
    case 7:
      // 上周
      start = -(7 + weekDay()) + 1;
      end = -weekDay();
      break;
    case 8:
      // 上月
      start = -(getDaysInCurrentMonth() + 1 + getDaysInLastMonth()) + 1;
      end = -getDaysInCurrentMonth();
      break;
    case 9:
      // 上季度
      start = -(getDaysSinceSeasonStart() + 1 + getLastQuarterDays()) + 1;
      end = -getDaysSinceSeasonStart();
      break;
    case 10:
      // 上年
      start = -(getDaysSinceStartOfYear() + 1 + getDaysInLastYear()) + 1;
      end = -getDaysSinceStartOfYear() - 1;
      break;
  }
  time = [formatDate(start, "start", "yyyy-MM-dd") + " 00:00", formatDate(end, "start", "yyyy-MM-dd") + " 00:00"];

  emits("changeDate", time);
};
</script>

<template>
  <div>
    <el-dropdown @command="changeCommand">
      <el-button size="small" type="primary">
        快捷<el-icon class="el-icon--right"><arrow-down /></el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item v-for="ele in commandList" :key="ele.key" :command="ele.key">{{ ele.name }}</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss" scoped></style>
